using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ShipUIPreviewer : MonoBehaviour
{
    public ShipUI shipUI { get { return m_ShipUI; } }
    UIDesigner m_UIDesigner;
    public Image m_LineObj;
    public List<Image> m_DragPoints = new List<Image>();
    ShipUI m_ShipUI;
    public bool m_IsEditMode;
    void Start()
    {
        m_IsEditMode = true;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void Init(ShipUI shipUI, UIDesigner uiDesigner)
    {
        m_UIDesigner = uiDesigner;
        m_ShipUI = shipUI;
    }
    public void SetID(int newID)
    {
        m_ShipUI.id = newID;
    }
    public void SetRaycastTarget(bool isEnabled)
    {
        m_LineObj.raycastTarget = isEnabled;
        foreach (var i in m_DragPoints)
        {
            i.raycastTarget = isEnabled;
        }
    }
    public void OnPressDown(BaseEventData eventData)
    {
        m_UIDesigner.OnShipUISelected(m_ShipUI);
    }
    public void OnDragBegin(BaseEventData eventData)
    {
        if (!Input.GetMouseButton(0))
            return;
        if (m_IsEditMode )
        {
            eventData.selectedObject = gameObject;
            if (m_UIDesigner)
            {
                m_UIDesigner.SetPreviewersRaycastTarget(false);
                m_UIDesigner.m_UIPreviewers.Remove(this);
            }
        }
        else
        {
            var shipDrager = ShipUIDrager.Create(m_ShipUI);
            eventData.selectedObject = shipDrager.gameObject;
        }
    }
    public void OnDraging(BaseEventData eventData)
    {
        if (!Input.GetMouseButton(0) || !eventData.selectedObject)
            return;
        if (m_IsEditMode)
        {
            eventData.selectedObject.transform.position = Input.mousePosition;
            m_ShipUI.positionScale = new Vector2(transform.localPosition.x / Screen.width, transform.localPosition.y / Screen.height);
        }
        else
        {
            eventData.selectedObject.transform.position = Input.mousePosition;
        }
    }

    public void OnDragEnd(BaseEventData eventData)
    {
        if (m_UIDesigner)
        {
            m_UIDesigner.SetPreviewersRaycastTarget(true);
        }
        if (!eventData.selectedObject) 
            return;
        if (m_IsEditMode)
        {
            Destroy(eventData.selectedObject);
            eventData.selectedObject = null;
        }
        if ((eventData.selectedObject.GetComponent<ShipUIPreviewer>() != null))
            eventData.selectedObject = null;
        if ((eventData.selectedObject.GetComponent<ShipUIDrager>() != null))
        {
            Destroy(eventData.selectedObject);
            eventData.selectedObject = null;
        }
    }
    public Vector3 m_TheBeginingPositionOfScalePoint;
    public Vector3 m_TheSizeBeforeExpand;
    public void OnScalePointDragBegin()
    {
        if (!m_IsEditMode || !Input.GetMouseButton(0))
            return;
        m_TheBeginingPositionOfScalePoint = Input.mousePosition;
        m_TheSizeBeforeExpand = GetComponent<RectTransform>().sizeDelta;
        m_UIDesigner.SetPreviewersRaycastTarget(false);
    }
    public void OnScalePointDraging()
    {
        if (!m_IsEditMode || !Input.GetMouseButton(0))
            return;

        var differenceValueCurrent = Input.mousePosition - m_TheBeginingPositionOfScalePoint;
        var magnitude = differenceValueCurrent.magnitude;


        var differenceValueAtTheFirst = m_TheBeginingPositionOfScalePoint - transform.position;
        if(differenceValueAtTheFirst.x>0 && differenceValueAtTheFirst.y > 0)
        {
            if (Vector3.Cross(differenceValueCurrent, new Vector3(-1, 1, 0)).z <= 0)
                magnitude *= -1;
        }
        else if(differenceValueAtTheFirst.x < 0 && differenceValueAtTheFirst.y > 0)
        {
            if (Vector3.Cross(new Vector3(1, 1, 0), differenceValueCurrent).z <= 0)
                magnitude *= -1;

        }
        else if (differenceValueAtTheFirst.x < 0 && differenceValueAtTheFirst.y < 0)
        {
            if (Vector3.Cross(differenceValueCurrent, new Vector3(-1, 1, 0)).z >= 0)
                magnitude *= -1;

        }
        else if (differenceValueAtTheFirst.x > 0 && differenceValueAtTheFirst.y < 0)
        {
            if (Vector3.Cross(new Vector3(1, 1, 0), differenceValueCurrent).z >= 0)
                magnitude *= -1;
        } 
        var newSize = new Vector3(m_TheSizeBeforeExpand.x + magnitude, m_TheSizeBeforeExpand.y + magnitude, m_TheSizeBeforeExpand.z);
        newSize = new Vector3(Mathf.Clamp(newSize.x, 32, 1024), Mathf.Clamp(newSize.y, 32, 1024), 0);
        GetComponent<RectTransform>().sizeDelta = newSize;
        m_ShipUI.GetComponent<RectTransform>().sizeDelta = newSize;
        shipUI.sizeScale = new Vector2(newSize.x / Screen.width, newSize.y / Screen.height);
    }
    public void OnScalePointDragEnd()
    {
        if (!m_IsEditMode)
            return;
        m_UIDesigner.SetPreviewersRaycastTarget(true);
    }
}
